#ifndef ACTIONFUNC_H
#define ACTIONFUNC_H
#include <stdlib.h>
#include <stdio.h>
#include <QDebug>
#include "defhead.h"


void doHoughCircle()
{
    std::cout<< "*********************"<<std::endl;
    Mat imat1 = imread(FILEPATH_CIRCLE);

    Mat tmpImg, imat;
    cvtColor (imat1, imat, CV_RGB2GRAY);

    GaussianBlur (imat,tmpImg, Size(9,9), 2,2);

    std::vector<Vec3f> vec;

    //param 5 ,累加器
    //param 6 ,mindst 圆心
    //param 7 ,canny thresh
    //param 8 ,圆心累加thresh
    HoughCircles (tmpImg, vec, CV_HOUGH_GRADIENT,2, 30,200, 150,0,0 );

    for(int i= 0;i < vec.size ();++i)
    {
        Point pt(vec[i][0], vec[i][1]);

        int radius = cvRound (vec[i][2]);

        circle(imat1, pt, 3, Scalar(0,0,255), -1, 8,0);
        circle(imat1, pt, radius, Scalar(0,0,255), 3, 8,0);
    }

    imshow("build",imat1);

    waitKey (0);
}

void doRemap()
{
    Mat imat = imread(FilEPATH);
    Mat mapx, mapy, dismat;

    if(imat.data == NULL)
    {
        std::cout<< "load image error"<<std::endl;
    }

    std::cout << imat.channels ()<<std::endl;

    dismat.create (imat.size(), imat.type ());
    mapx.create (imat.size(),CV_32FC1);
    mapy.create (imat.size(),CV_32FC1);

    for(int i =0 ;i<imat.rows;++i)
    {
        for(int j= 0; j<imat.cols; ++j)
        {
            mapx.at<float>(i,j) = j;
            mapy.at<float>(i,j) = imat.rows - i;
        }
    }

    remap(imat, dismat, mapx,mapy,CV_INTER_LINEAR );

    imshow ("origin",imat);
    imshow ("dst",dismat);
    waitKey (0);
}



#define WINDOW_NAME1 "ORIGINE_IMAGE"
#define WINDOW_NAME2 "WRAP_IMAGE"
#define WINDOW_NAME3 "ROTATION"

void doAffine()
{
    Mat imat = imread(FilEPATH);

    system ("color 1A");
    std::cout<< "wrap affine and rotation affine"<<std::endl;

    if(imat.data == NULL)
        return;

    imshow (WINDOW_NAME1,imat);

    Mat wrapMat(2,3,CV_32FC1);
    Mat ditImg;

    Point2f srcpt[3];
    Point2f dstpt[3];

    srcpt[0]=Point(0,0);
    srcpt[1]=Point(static_cast<float>(imat.cols - 1), 0);
    srcpt[2]=Point(0,static_cast<float>(imat.rows -1));

    dstpt[0]=Point(static_cast<float>(imat.cols*0.0),static_cast<float>(imat.rows*0.33));
    dstpt[1]=Point(static_cast<float>(imat.cols*0.65),static_cast<float>(imat.rows*0.35));
    dstpt[2]=Point(static_cast<float>(imat.cols*0.15),static_cast<float>(imat.rows*0.6));

    wrapMat = getAffineTransform (srcpt,dstpt);
    warpAffine (imat, ditImg,wrapMat,imat.size());

    imshow (WINDOW_NAME2, ditImg);
    //-----------------------------------------------------------------------------------
    Point center(ditImg.cols/2, ditImg.rows/2);

    double angle = -30.0;
    double scale = 0.8;

    Mat rotamat = getRotationMatrix2D (center, angle, scale);

    Mat rotaImg;

    warpAffine (ditImg, rotaImg, rotamat, ditImg.size());

    imshow (WINDOW_NAME3, rotaImg);
    waitKey (0);

}
#endif // ACTIONFUNC_H


